<template>
	<view class="page">
		<!-- 顶部下拉选择 -->
		<!-- <u-sticky>
			<view v-if="!isSelect" class="top-select">
				<view class="select-item" @click="selectCity()">
					<view class="select-title">{{ city.title || '选择您的城市' }}</view>
					<image class="select-icon" src="/static/images/drop-downhs@3x.png"></image>
				</view>
				<view class="select-item" @click="chooseHospital">
					<view class="select-title">{{ params.hospitalType || '医院筛选' }}</view>
					<image class="select-icon" src="/static/images/drop-downhs@3x.png"></image>
				</view>
			</view>
		</u-sticky> -->
		<!-- 医院列表数据 -->
		<view class="hospital-list">
			<view class="hospital-item" v-for="(item, index) in list" :key="index" @click="toDetail(item)">
				<image class="hospital-image" :src="item.icon || '/static/images/ic_img.png'" mode="aspectFill"></image>
				<view class="hospital-info">
					<view class="hospital-title">{{ item.hospitalName }}</view>
					<view class="hospital-tab-list">
						<view class="tab-item">{{ item.hospitalGrade }}</view>
						<view class="tab-item" style="background-color: #49ABF7;">{{ item.hospitalType }}</view>
					</view>
					<view class="hospital-address">{{ item.hospitalAddress }}</view>
				</view>
			</view>
		</view>
		<!-- 弹窗 -->
		<u-picker :show="show" :columns="hospitalList" keyName="text" @cancel="show = false" @confirm="confirm"></u-picker>
	</view>
</template>

<script>
	import {
		gethotHospital
	} from '@/apis/request.js';

	export default {
		data() {
			return {
				show: false,
				isSelect: false,
				total: 0,
				list: [],
				city: {
					id: 350200,
					title: '厦门市'
				},
				hospitalList: [
					[{
							text: '全部',
							isChecked: false
						},
						{
							text: '综合医院',
							isChecked: false
						},
						{
							text: '妇幼保健院',
							isChecked: false
						},
						{
							text: '儿童医院',
							isChecked: false
						},
						{
							text: '口腔医院',
							isChecked: false
						},
						{
							text: '眼科医院',
							isChecked: false
						},
						{
							text: '心血管病医院',
							isChecked: false
						},
						{
							text: '精神病医院',
							isChecked: false
						}
					]
				],
				params: {
					cityId: '',
					hospitalType: '',
					pageNo: 1,
					pageSize: 10
				}
			};
		},
		onLoad(options) {
			this.isSelect = options.isSelect;
			this.city = uni.getStorageSync('city');
			this.getList();
		},
		onReachBottom() {
			if (this.list.length < this.total) {
				this.getList();
			}
		},
		onPullDownRefresh() {
			this.list = [];
			this.params.pageNo = 1;
			this.getList();
		},
		methods: {
			chooseHospital() {
				this.show = true;
			},
			confirm(e) {
				this.show = false;
				const type = e.value[0].text;
				this.params.hospitalType = type == '全部' ? '' : type;
				this.params.pageNo = 1;
				this.getList();
			},
			toDetail(item) {
				if (this.isSelect) {
					uni.$emit('selectHospital', item);
					uni.navigateBack();
				} else {
					uni.navigateTo({
						url: '/pages/IndexPages/hospitalDetails?id=' + item.id
					});
				}
			},
			//选择城市
			selectCity() {
				let that = this;
				uni.navigateTo({
					url: '/pages/IndexPages/ChooseCity/main',
					events: {
						changeCity(data) {
							that.city = data;
							that.params.pageNo = 1;
							that.list = [];
							that.getList();
						}
					}
				});
			},
			to(url) {
				uni.navigateTo({
					url
				});
			},
			getList() {
				this.params.cityId = this.city.id;
				gethotHospital(this.params).then(res => {
					if (this.params.pageNo == 1) {
						this.total = res.total;
						this.list = res.records;
					} else {
						this.list = this.list.concat(res.records);
					}
					if (this.list.length < this.total) {
						this.params.pageNo++;
					}
				}).finally(() => {
					uni.stopPullDownRefresh();
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.page {
		height: 100%;
		min-height: 100vh;
		background-color: #f2f2f2;

		.top-select {
			background-color: #fff;
			width: 750rpx;
			height: 88rpx;
			line-height: 88rpx;
			border-bottom: 2rpx solid #e8e8e8;
			display: flex;
			align-items: center;
			justify-content: space-around;

			.select-item {
				display: flex;
				align-items: center;

				.select-title {
					color: #666666;
					font-size: 26rpx;
				}

				.select-icon {
					margin-left: 6rpx;
					width: 16rpx;
					height: 10rpx;
				}
			}
		}

		.hospital-list {
			background-color: #fff;

			.hospital-item {
				display: flex;
				align-items: center;
				padding: 30rpx;
				border-bottom: 2rpx solid #e8e8e8;

				.hospital-image {
					flex-shrink: 0;
					height: 144rpx;
					width: 144rpx;
					border-radius: 10rpx;
				}

				.hospital-info {
					padding-left: 20rpx;

					.hospital-title {
						font-weight: bold;
						color: #333333;
						font-size: 30rpx;
					}

					.hospital-tab-list {
						display: flex;
						margin-top: 16rpx;

						.tab-item {
							background-color: #f4a200;
							color: #fff;
							font-size: 20rpx;
							padding: 6rpx 10rpx;
							margin-right: 10rpx;
							border-radius: 6rpx;
						}
					}

					.hospital-address {
						margin-top: 20rpx;
						font-size: 24rpx;
						color: #666666;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
					}
				}
			}
		}
	}
</style>